package com.bqtt.tumor.demo.presentation.controller;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bqtt.tumor.demo.business.DeseaseService;
import com.bqtt.tumor.demo.business.HealthUserService;
import com.bqtt.tumor.demo.business.MedicalCaseService;
import com.bqtt.tumor.demo.common.utility.InlineTools;
import com.bqtt.tumor.demo.entity.HealthUser;
import com.bqtt.tumor.demo.entity.MedicalCase;
import com.bqtt.tumor.demo.entity.SystemUser;
import com.bqtt.tumor.demo.presentation.model.medical_case.MedicalCondition;

public class medical_case {

	public static String queryMedicalCase(SystemUser user,  ModelMap model, MedicalCase case_condition,String id_number)
	{
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		if(case_condition==null)
		{
			case_condition = new MedicalCase();
		}
		
		if(!InlineTools.isEmpty(id_number))
		{
			case_condition.setHealth_user_id_number(id_number);;
		}
		
		
		if(!model.containsAttribute("new_medical_case"))
		     model.addAttribute("new_medical_case", new MedicalCase());		
		if(!model.containsAttribute("edit_medical_case"))
		     model.addAttribute("edit_medical_case", new MedicalCase());	

		//添加疾病列表
		model.addAttribute("desease_list",DeseaseService.getDeseaseList());
		
		model.addAttribute("historySearchCondition", case_condition);
		
		model.addAttribute("condition", case_condition); //保留上次查找的字段
		
	    model.addAttribute("medical_case_list", MedicalCaseService.searchByCondition(case_condition, 20, 1));
		
	    model.addAttribute("isAddNewShow","false");
		
		return "admin/medical_case";
	}
	
	
	public static String addMedicalCase(SystemUser user,  ModelMap model, MedicalCase new_medical_case,BindingResult bindingResult)
	{
		MedicalCase historySearchCondition = (MedicalCase) model.get("historySearchCondition");
		if(historySearchCondition==null)
			historySearchCondition = new MedicalCase();
		
		//添加疾病列表
		model.addAttribute("desease_list",DeseaseService.getDeseaseList());
		
		if(!model.containsAttribute("new_medical_case"))
		     model.addAttribute("new_medical_case", new MedicalCase());		
		if(!model.containsAttribute("edit_medical_case"))
		     model.addAttribute("edit_medical_case", new MedicalCase());
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			//出错的话，就保持弹出窗口，显示错误
			
			model.addAttribute("isAddNewShow","true");
			// 其他变量需要手动添加到Model
			model.addAttribute("medical_case_list", MedicalCaseService.searchByCondition(historySearchCondition, 20, 1));
			
			model.addAttribute("condition", historySearchCondition);
			
			return "admin/medical_case";
		}
		
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("currentUser"))
			{
				model.addAttribute("currentUser", new SystemUser());
			}
			return "login";
		}
		
		boolean result = MedicalCaseService.add(new_medical_case);
		
		if(result)
		{
			model.addAttribute("new_medical_case", new MedicalCase());		
			model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
		}
		else
		{
			model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
		}
		
		
		
		// 其他变量需要手动添加到Model
		model.addAttribute("condition", historySearchCondition);
		model.addAttribute("medical_case_list", MedicalCaseService.searchByCondition(historySearchCondition, 20, 1));
	    model.addAttribute("isAddNewShow","false");

		return "admin/medical_case";
	}


	public static String editMedicalCase(SystemUser user,  ModelMap model,MedicalCase edit_medical_case,BindingResult bindingResult)
	{
		MedicalCase historySearchCondition = (MedicalCase) model.get("historySearchCondition");
		if(historySearchCondition==null)
			historySearchCondition = new MedicalCase();
		
		//添加疾病列表
		model.addAttribute("desease_list",DeseaseService.getDeseaseList());
		
		if(!model.containsAttribute("new_medical_case"))
		     model.addAttribute("new_medical_case", new MedicalCase());		
		if(!model.containsAttribute("edit_medical_case"))
		     model.addAttribute("edit_medical_case", new MedicalCase());
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			//出错的话，就保持弹出窗口，显示错误
			
			model.addAttribute("isAddNewShow","true");
			// 其他变量需要手动添加到Model
			model.addAttribute("medical_case_list", MedicalCaseService.searchByCondition(historySearchCondition, 20, 1,edit_medical_case.getMedical_case_id()));
			
			model.addAttribute("condition", historySearchCondition);
			
			return "admin/medical_case";
		}
		
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("currentUser"))
			{
				model.addAttribute("currentUser", new SystemUser());
			}
			return "login";
		}
		
		boolean result = MedicalCaseService.update(edit_medical_case);
		
		if(result)
		{
			model.addAttribute("edit_medical_case", new MedicalCase());		
			model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
		}
		else
		{
			model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
		}
		
		
		
		// 其他变量需要手动添加到Model
		model.addAttribute("condition", historySearchCondition);
		model.addAttribute("medical_case_list", MedicalCaseService.searchByCondition(historySearchCondition, 20, 1));
	    model.addAttribute("isAddNewShow","false");

		return "admin/medical_case";
	}
}
